package tr.com.argela.tap.mot.servlets;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import tr.com.argela.tap.mot.dao.DbManager;
import tr.com.argela.tap.mot.dao.VersionDao;
import tr.com.argela.tap.mot.logger.LoggerFactory;
import tr.com.argela.tap.mot.models.Project;
import tr.com.argela.tap.mot.util.CommonUtil;

/**
 * @author arge
 */
public class VersionServlet extends HttpServlet {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    private static Logger logger = LoggerFactory.getLogger("servlets");

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("Process request called");

        DbManager dbManager = new DbManager();

        try {
            String projectId = request.getParameter("projects");
            String versionName = request.getParameter("vname");

            String verDescription = request.getParameter("vdescription");
            // String relase = request.getParameter("relase");
            String releaseDate = request.getParameter("endDate");

            Date relasedate = CommonUtil.parseDate(releaseDate);

            if (projectId != null && verDescription != null && versionName != null && relasedate != null) {
                VersionDao addDao = new VersionDao();
                System.out.print("Version details" + versionName + verDescription + projectId + releaseDate);

                Project project = new Project();
                project.setId(Long.parseLong(projectId));
                addDao.addProjectVersion(dbManager, versionName, verDescription, project, relasedate);
            }
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }
        response.sendRedirect("home.jsp");

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * 
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
